package com.red.wood.module.eval.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.dao.SingleTaskDao;
import com.red.wood.module.eval.model.SingleTask;

@Repository
public class SingleTaskDaoImpl implements SingleTaskDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private CommonDao dao;

	@Override
	public void add(List<Map<String,Object>> paramList) {
		String columns = "userId,name,type,applyBusiness,businessScope,statue,lastSubmitTime,createTime,form_number";
		String insertSql = "insert into t_eval_singleTask(userId,name,type,applyBusiness,businessScope,statue,lastSubmitTime,createTime,form_number) values (?,?,?,?,?,?,?,?,?)";
		dao.insertBatchTableData(paramList, insertSql, columns);
	}
	
	@Override
	public void delete(String id) {
		StringBuffer sql = new StringBuffer();
		if(StringUtils.isNotBlank(id)){
			sql.append("delete from t_eval_singleTask where id = "+ id);
		}
		dao.deleteTableData(sql.toString());
	}

	@Override
	public void update(IMap param) {
		String id = CommonUtils.valueOf(param.get("id"));
		if(StringUtils.isNotBlank(id)){
			StringBuilder sqlSb = new StringBuilder();
			sqlSb.append("update t_eval_singleTask set ");
			String queryConf = " where id=" + id;
			dao.updateTableData(param, sqlSb, queryConf);
		}
	}

	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = listCount(param);
		list = listPageData(param);
		if(list != null && !list.isEmpty()){
			for(Map<String, Object> map: list){
				if(map.get("lastSubmitTime") != null){
					Date lastSubmitTime = (Date)map.get("lastSubmitTime");
					SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("lastSubmitTime", formatter.format(lastSubmitTime));
				} else {
					map.put("lastSubmitTime", "无");
				}
				String businessScope = CommonUtils.valueOf(map.get("businessScope"));
				if(!businessScope.equals("0")){
					String applyBusiness = CommonUtils.valueOf(map.get("applyBusiness"));
					if(businessScope.equals("1")){
						map.put("applyBusiness", applyBusiness + ":" + SingleTask.EVAL_SINGLETASK_BUSINESSSCOPE_AREA);
					} else if(businessScope.equals("2")){
						map.put("applyBusiness", applyBusiness + ":" + SingleTask.EVAL_SINGLETASK_BUSINESSSCOPE_PROINCE);
					}
				}
			}
		}
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		Page page = (Page)param.get("page");
		int offset = (page.getPageNo() -1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		sql.append("select id as id, userId as userId, name as name, type as type, applyBusiness as applyBusiness, businessScope as businessScope, statue as statue, lastSubmitTime as lastSubmitTime, createTime as createTime,form_number from t_eval_singleTask where 1=1");
		sql.append(getQueryconf(param));
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) as count from t_eval_singleTask where 1=1");
		sql.append(getQueryconf(param));
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryconf(Map<String, Object> param){
		StringBuffer queryConf = new StringBuffer();
		String name = CommonUtils.valueOf(param.get("name"));
		if(StringUtils.isNotBlank(name)){
			queryConf.append(" and name like concat('%', '"+name+"', '%')");
		}
		String userId = CommonUtils.valueOf(param.get("userId"));
		if(StringUtils.isNotBlank(userId)){
			queryConf.append(" and userId=" + userId);
		} else {
			queryConf.append(" and userId=" + 1);
		}
		String statue = CommonUtils.valueOf(param.get("statue")); // 表单编号
		if(StringUtils.isNotBlank(statue)){
			queryConf.append(" and statue =" + statue);
		}
		queryConf.append(" order by form_number desc,createTime desc");
		return queryConf;
	}

	@Override
	public long count(Map<String, Object> param) {
		return 0;
	} 
	
	public Map<String, Object> getById(String id){
		if(StringUtils.isNotBlank(id)){
			String sql = "select id as id, userId as userId, name as name, type as type, applyBusiness as applyBusiness, businessScope as businessScope, statue as statue, lastSubmitTime as lastSubmitTime, createTime as createTime from t_eval_singleTask where id=" + id;
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
			if(null != list && list.size() > 0){
				return list.get(0);
			}
		}
		return null;
	}
	
}
